Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

297
Vistas
Code to create download link which expire using nodejs or express [frontend and backend]

I'm new here. I just started using node.js - express.js I want to add download link which expire after certain time to my website. I'm struggling to code this since a week. Anyone who knows please provide the code [frontend and backend], it will be a huge help.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Check this simple implementation:

You store the information of the download in a file. The filename is the download session id. The file content is the real path of the file to be downloaded.

Use these three functions to manage the life cycle of the download sessions:

    /* Gets the download file path related to a download sid */
      function getDownloadFilePath(downloadSid, callback) {
    // Get the download session file name
       var dlSessionFileName = path.join(DL_SESSION_FOLDER, downloadSid + 
        '.download');

    // Check if the download session exists
    if (!fs.existsSync(dlSessionFileName)) return callback(new 
    Error('Download does not exist'));

    // Get the file path
    fs.readFile(dlSessionFileName, function(err, data) {
      if (err) return callback(err);

    // Return the file path
     callback(null, data);
    });
   }

   /* Deletes a download session */
    function deleteDownload(downloadSid, callback) {
     // Get the download session file name
    var dlSessionFileName = path.join(DL_SESSION_FOLDER, downloadSid + '.download');

  // Check if the download session exists
  if (!fs.existsSync(dlSessionFileName)) return callback(new Error('Download does not exist'));

  // Delete the download session
  fs.unlink(dlSessionFileName, function(err) {
    if (err) return callback(err);

    // Return success (no error)
    callback();
  });
}

Use createDownload() to create download sessions wherever you need to. It returns the download sid, then you can use it to build your download URL like: http://your.server.com/download?sid=.

Finally you can add a simple handler to your /download route:

app.get('/download', function(req, res, next) {
  // Get the download sid
  var downloadSid = req.query.sid;

  // Get the download file path
  getDownloadFilePath(downloadSid, function(err, path) {
    if (err) return res.end('Error');

    // Read and send the file here...

    // Finally, delete the download session to invalidate the link
    deleteDownload(downloadSid, function(err) {
      // ...
    });
  });
});

With this method, you don't have to create/move/delete big download files, which could cause slow responses and unnecessary resource consumption.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda